<template>
	<view>
		<mc-framework navTitle="MES工控机" @changeLevel="changeLevel">

			<template #content>
				<view class="content" style="height: 100%;">
					<view class="contentBox">
						<view class="container">
							<uni-forms ref="baseForm" :modelValue="baseFormData">
								<view class="form-item-row">

									<view class="custom-cell">申请人填写</view>


									<uni-forms-item label="机台:" label-width="auto">
										<input v-model="baseFormData.EquipmentName" type="text" placeholder="机台"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="申请人:" label-width="auto">
										<input v-model="baseFormData.ApplyForMan" type="text" disabled
											placeholder="申请人" />
									</uni-forms-item>
									<uni-forms-item label="申请时间:" label-width="auto">
										<input v-model="baseFormData.ApplyForTime" type="text" disabled />
									</uni-forms-item>

									<uni-forms-item label="故障描述:" label-width="auto" style="width: 95.5vw;" required>
										<input v-model="baseFormData.BXContent" type="text" placeholder="请输入故障描述"
											style="width: 85vw;" :disabled='buttonTitle == "已维修"' />
									</uni-forms-item>

									<!-- <u-cell title="维修人填写" class="custom-cell"></u-cell> -->
									<view class="custom-cell" v-show="buttonTitle!='保存'">维修人填写</view>

									<uni-forms-item label="维修编号:" label-width="auto" v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.BillNO" type="text" placeholder="自动带出维修编号"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="维修人:" label-width="auto" required v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.MaintainMan" type="text" disabled
											placeholder="请选择人员" />
										<text class="text" @click="openBaseScreen('人员')"></text>
									</uni-forms-item>
									<uni-forms-item label="维修内容:" label-width="auto" required
										v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.GZCause" type="text" placeholder="请输入维修内容"
											:disabled='buttonTitle == "已维修"' />
									</uni-forms-item>
									<uni-forms-item label="维修方式:" label-width="auto" required
										v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.WayName" type="text" placeholder="请选择维修方式"
											disabled />
										<text class="text" @click="addWay('WXFS')"></text>
									</uni-forms-item>
									<uni-forms-item label="维修费用:" label-width="auto" :required='buttonTitle == "结束维修"'
										v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.Cost" type="number"
											:disabled='buttonTitle == "开始维修"||buttonTitle == "已维修"'
											:placeholder="buttonTitle == '结束维修' ?'请输入维修费用':'请在结束时填写'" />
									</uni-forms-item>
									<uni-forms-item label="维修状态:" label-width="auto" v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.WCStatus?'已维修':'未维修'" type="text"
											placeholder="自动带出维修状态" disabled />
									</uni-forms-item>
									<uni-forms-item label="维修结果:" label-width="auto" :required='buttonTitle == "结束维修"'
										v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.FResultName" type="text"
											:placeholder="buttonTitle == '结束维修' ?'请选择维修结果':'请在结束时填写'" disabled />
										<text class="text" @click="addWay1('SBWX')"></text>
									</uni-forms-item>
									<uni-forms-item label="维修开始时间:" label-width="auto" v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.BeginTime" type="text" placeholder="自动带出开始时间"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="维修结束时间:" label-width="auto" v-show="buttonTitle!='保存'">
										<input v-model="baseFormData.WCTime" type="text" placeholder="自动带出结束时间"
											disabled />
									</uni-forms-item>
								</view>

							</uni-forms>

							<view class="buttonView">
								<button size="mini" class="buttonstyle" @click="addplus()">新增</button>
							</view>
							<view class="table">
								<table>
									<tr>
										<th width='15%'>操作</th>
										<th>配件名称</th>
										<th>数量</th>
									</tr>
									<tr v-for="(item, index) in tableData">
										<td>
											<uni-icons type="trash" size="30" @click="trash(index)"></uni-icons>
										</td>
										<td><input type="text" v-model="item.ProductPJName" disabled
												placeholder="请选择配件"></input>
											<text class="text" @click="addproducplname(item,index)"></text>
										</td>
										<td><input type="number" v-model="item.Num"></input></td>
									</tr>
								</table>
							</view>
						</view>


					</view>
				</view>
				<view class="bottomBtnsOne">
					<button block @click="submit" v-show="buttonShow">
						{{buttonTitle}}
					</button>
				</view>

				<u-action-sheet :actions="StateList" :show="StateShow" @select="StateClick"
					@close="StateShow=false"></u-action-sheet>
				<u-action-sheet :actions="producplnameList" :show="producplnameShow" @select="producplnameClick"
					@close="producplnameShow=false"></u-action-sheet>
				<u-toast ref="uToast"></u-toast>


				<u-popup :show="popup_logout" mode="center" :closeOnClickOverlay="false" :round="4">
					<view class="popup-content">
						<view class="popup-content_close x-c">
							<uni-icons type="closeempty" color="#999" size="20"
								@click='popup_logout = false'></uni-icons>
						</view>
						<view class="x-c title">
							<text>{{popuptitle}}</text>
						</view>
						<view class="contentView">
							<text>{{popupcontent}}</text>
						</view>
						<view class="btnBox x-ac">
							<u-button type="primary" text="确定" @click="confirmClick"></u-button>
						</view>
					</view>
				</u-popup>
			</template>

		</mc-framework>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime
	} from '@/mes/common/base.js';
	export default {
		data() {
			return {
				baseFormData: {
					EquipmentName: '',
					ApplyForMan: '',
					ApplyForTime: '',
					BXContent: '',
					BillNO: '',
					MaintainMan: '',
					GZCause: '',
					Way: '',
					WayName: '',
					Cost: '',
					WCStatus: '',
					FResult: 0,
					FResultName: '',
					BeginTime: '',
					WCTime: '',
					BillDate: '',

				},
				sexs: [{
					text: '合格',
					value: 0
				}, {
					text: '不合格',
					value: 1
				}],
				buttonShow: true,
				buttonTitle: '保存',
				tableData: [],

				StateShow: false,
				StateList: [],
				producplnameIndex: 0,
				producplnameShow: false,
				producplnameList: [],
				modalcontent: '',
				popup_logout: false,
				popuptitle: '',
				popupcontent: ''
			};
		},
		onLoad(item) {
			var that = this

			if (item.MaintainEquipmentID) {
				that.queryList(item.MaintainEquipmentID)
			} else {
				if (item.item) {
					var item = JSON.parse(item.item)
					this.baseFormData = item
					this.baseFormData.BXContent = item.FaultName + ':' + item.GZContent
				}
				this.baseFormData.ApplyForMan = this.baseFormData.ApplyForMan ? this.baseFormData.ApplyForMan : this
					.$store.state.UserInfo.UserName
				console.log(this.baseFormData.ApplyForMan);
				this.baseFormData.CheckupID = this.baseFormData.CheckupID ? this.baseFormData.CheckupID : this.$store
					.state.UserInfo.EmplID
				this.baseFormData.EquipmentName = this.baseFormData.EquipmentName ? this.baseFormData.EquipmentName : this
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
				console.log(this
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName);
				console.log(this
					.$store.state.GuyueModule.MesBasicInfo);
				this.baseFormData.EquipmentID = this.baseFormData.EquipmentID ? this.baseFormData.EquipmentID : this
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
				this.baseFormData.ApplyForTime = this.baseFormData.ApplyForTime ? this.baseFormData.ApplyForTime :
					getFormatDateTime(new Date())
				this.baseFormData.BillDate = this.baseFormData.BillDate ? this.baseFormData.BillDate :
					getFormatDateTime(new Date())
			}
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		onShow() {
			this.getValue
		},
		methods: {
			confirmClick() {
				this.popup_logout = false
			},
			addPeriodTypeClick() {
				var that = this
				if (!that.baseFormData.BeginTime) {
					that.buttonTitle = '开始维修'
				} else if (that.baseFormData.BeginTime) {
					that.buttonTitle = '结束维修'
				}
				if (that.baseFormData.MaintainEquipmentID && that.baseFormData.BeginTime && that.baseFormData
					.WCTime) {
					that.buttonTitle = '已维修'
					that.buttonShow = false
				}
				that.$forceUpdate()
			},
			trash(index) {
				var that = this
				that.tableData.splice(index, 1)
				this.$forceUpdate()
			},
			addplus() {
				var that = this
				var obj = {
					Num: 0,
					ProductPJName: '',
					ProductPJID: null,
					MaintainEquipmentDeID: 0
				}
				that.tableData.push(obj)
				this.$forceUpdate()
			},
			producplnameClick(e) {
				var that = this;
				console.log(e);
				that.tableData[that.producplnameIndex].ProductPJID = e.ProductPJID
				that.tableData[that.producplnameIndex].ProductPJName = e.ProductPJName
				this.$forceUpdate()
			},
			addproducplname(item, index) {
				var that = this;
				that.producplnameIndex = index

				that.Request.request('getsProductPJ', {

				}, function(ret) {
					console.log(ret.data.ds);
					if (ret.status == 200) {
						ret.data.ds.map(item => item.name = item.ProductPJName)
						that.producplnameList = ret.data.ds
						that.producplnameShow = true
					}
				})
			},
			StateClick(e) {
				var that = this;
				console.log(e);
				console.log(that.StateName);
				if (that.StateName == 'SBWX') {
					console.log(e.id);
					that.baseFormData.FResult = e.id
					that.baseFormData.FResultName = e.name
					that.$forceUpdate()
				} else {
					console.log(that.StateName);
					that.baseFormData.WayName = e.name
					that.baseFormData.Way = e.id
					that.$forceUpdate()
				}
				this.$forceUpdate()
			},
			addWay1(str) {
				var that = this;
				if (that.buttonTitle == '结束维修' && that.buttonShow) {
					that.StateName = str

					that.Request.request('getViewState', {
						FType: str
					}, function(ret) {

						if (ret.status == 200) {
							that.StateList = ret.data.ds
						}
					})
					this.StateShow = true
				}
			},
			addWay(str) {
				var that = this;
				if (that.buttonShow) {
					that.StateName = str

					that.Request.request('getViewState', {
						FType: str
					}, function(ret) {

						if (ret.status == 200) {
							that.StateList = ret.data.ds
						}
					})
					this.StateShow = true

				}
			},
			getValue(e) {
				if (e != '返回') {
					console.log(e);
					this.baseFormData.MaintainMan = e.name
					// this.baseFormData.EquipmentID = e.EquipmentID
					this.$forceUpdate()
				}
			},
			openBaseScreen(str) {

				var that = this;
				var obj;
				if (that.buttonShow) {
					if (str == '人员') {
						obj = {

							type: str,
							cmd: 'getEmplByJobName',
							param: {
								JobName: '',
								PageIndex: 1,
								PageSize: 30
							}
						}
					}

					uni.navigateTo({
						url: `../common/baseScreen?obj=${JSON.stringify(obj)}`,
						animationType: 'slide-in-right',
						animationDuration: 300
					});

				}

			},



			submit() {
				var that = this
				var msg
				var show = true

				if (that.baseFormData.BXContent == '' || that.baseFormData.BXContent == null) {
					msg = '请输入故障描述'
					show = false
				}
				if (that.buttonTitle == '开始维修') {
					if (that.baseFormData.MaintainMan == '' || that.baseFormData.MaintainMan == null) {
						msg = '请选择人员'
						show = false
					} else if (that.baseFormData.GZCause == '' || that.baseFormData.GZCause == null) {
						msg = '请输入维修内容'
						show = false
					} else if (that.baseFormData.WayName == '' || that.baseFormData.WayName == null) {
						msg = '请选择维修方式'
						show = false
					}
				} else if (that.buttonTitle == '结束维修') {
					if (that.baseFormData.FResultName == '' || that.baseFormData.FResultName == null) {
						msg = '请选择维修结果'
						show = false
					} else if (that.baseFormData.Cost == '' || that.baseFormData.Cost == null) {
						msg = '请输入维修费用'
						show = false
					}
				}
				if (show) {
					var obj = {
						MaintainEquipmentID: that.baseFormData.MaintainEquipmentID ? that.baseFormData
							.MaintainEquipmentID : 0,
						ApplyForMan: that.baseFormData.ApplyForMan,
						EquipmentID: that.baseFormData.EquipmentID,
						ApplyForTime: that.baseFormData.ApplyForTime,
						BXContent: that.baseFormData.BXContent,
						BillNO: that.baseFormData.BillNO,
						MaintainMan: that.baseFormData.MaintainMan,
						GZCause: that.baseFormData.GZCause,
						Way: that.baseFormData.Way,
						Cost: that.baseFormData.Cost ? that.baseFormData.Cost : 0,
						WCStatus: that.baseFormData.WCStatus,
						FResult: that.baseFormData.FResult ? that.baseFormData.FResult : 0,
						BeginTime: that.baseFormData.BeginTime,
						WCTime: that.baseFormData.WCTime,
						BillDate: that.baseFormData.BillDate,
						FType: 'SBWX',
						Num: that.tableData.map(item => item.Num).join(','),
						ProductPJID: that.tableData.map(item => item.ProductPJID).join(','),
						MaintainEquipmentDeID: that.tableData.map(item => item.MaintainEquipmentDeID).join(
							','),
						ProductPJName: that.tableData.map(item => item.ProductPJName).join(','),
					}

					console.log(obj);
					that.Request.request('saveMaintainEquipment', obj, function(ret) {
						console.log(ret);
						if (ret.status == 200) {
							that.$refs.uToast.show({
								title: '保存',
								type: 'success',
								message: '保存成功'
							})
							that.queryList(ret.data.ds[0].MaintainEquipmentID)
						}
					})
				} else {
					that.popuptitle = '报错'
					that.popupcontent = msg
					that.popup_logout = true
				}
			},
			queryList(id) {
				var that = this
				try {
					that.Request.request('getMaintainEquipmentDetail', {
						MaintainEquipmentID: id
					}, (ret) => {
						console.log(ret.data);
						ret.data.ds.map((item) => {
							item.ApplyForTime = item.ApplyForTime ? item.ApplyForTime.replace('T',
									' ') :
								''
							item.BeginTime = item.BeginTime ? item.BeginTime.replace('T', ' ') : ''
							item.WCTime = item.WCTime ? item.WCTime.replace('T', ' ') : ''
						})
						that.baseFormData = ret.data.ds[0]

						that.tableData = ret.data.ds1
						that.addPeriodTypeClick()
						that.$forceUpdate()

					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},


			changeLevel(data, row) {
				let routes = getCurrentPages(); //当前页面栈
				let backIndex = routes.length - row.length; // 计算返回页面栈数
				if (backIndex == 1) {
					let prevPage = routes[routes.length - 2]
					prevPage.$vm.getList()
				}
				uni.navigateBack({
					delta: backIndex
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";

	.custom-cell {

		padding: 0.5% 1.5% !important;
		width: 100vw;
		font-size: 14px;
		font-weight: bold !important;
	}




	.buttonView {
		float: right;
		padding: 1%;

		.buttonstyle {
			color: #fff;
			background-color: #51d0d0;
		}
	}


	.table table {
		table-layout: fixed;

		tr td {
			input {
				width: 100%;
				background-color: #e7e7e7 !important;
			}
		}
	}
</style>